package com.danger.Model.action;

import android.content.Context;

import com.danger.Model.bean.ObjectInfo;
import com.danger.Model.listener.OnSearchListener;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by danger on 2016/4/3.
 */
public class SearchLostModelImpl implements SearchLostModel {

    private Context context;

    public SearchLostModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void searchByCategory(String category, final OnSearchListener listener) {
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.addWhereEqualTo("category", category);
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(s);
            }
        });
    }

    @Override
    public void searchByTitle(String title, final OnSearchListener listener) {
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.addWhereEqualTo("title", title);
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(s);
            }
        });
    }

    @Override
    public void searchByIndistinct(String key, String value, final OnSearchListener listener) {
        BmobQuery<ObjectInfo> query = new BmobQuery<ObjectInfo>();
        query.addWhereContains(key, value);
        query.order("-date");
        query.findObjects(context, new FindListener<ObjectInfo>() {
            @Override
            public void onSuccess(List<ObjectInfo> list) {
                listener.onSuccess(list);
            }

            @Override
            public void onError(int i, String s) {
                listener.onError(s);
            }
        });
    }
}
